package org.mparts.client.ui;

import org.mparts.client.content.Images;
import org.mparts.client.content.Strings;
import org.mparts.client.place.AddressPlace;
import org.mparts.client.place.CatalogPlace;
import org.mparts.client.place.MainPlace;
import org.mparts.client.utils.Utils;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.IFrameElement;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.NorthSouthContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.HasSelectHandlers;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.toolbar.LabelToolItem;

public class MenuView extends ResizeComposite implements IMenuView {

	public interface Binder extends UiBinder<Widget, MenuView> {
	}
	
	@UiField
	TextButton searchButton;
	@UiField
	TextField searchField;

	private IMenuPresenter presenter;
	
	private Images images;
	private Strings strings;
	
	@UiField
	TextButton mainButton;

	@UiField
	TextButton catalogButton;
	
	@UiField
	TextButton loadCatalogButton;
	
	@UiField
	TextButton addressButton;
//	private VBoxLayoutContainer vBox;
	
	@UiField
	TextButton loginButton;
	
	@UiField
	LabelToolItem userName;
	
	@UiField(provided = true)
	Image logoImg;
	
	@UiField
	IFrameElement weatherFrame;
	
//	private String loginURL = null;
//	private String logoutURL = null;
	
	@UiField
	NorthSouthContainer cont;
	
	@Inject
	public MenuView( Images images, Strings strings, Binder uiBinder ) {
		this.images = images;
		this.strings = strings;
		logoImg = new Image(Utils.getCurrentStoreId() == 2L ? images.logotor() : images.logo());
		cont = (NorthSouthContainer) uiBinder.createAndBindUi(this);//new NorthSouthContainer();
		weatherFrame.setSrc(Utils.getCurrentStoreId() == 2L ? "weathertor.html" : "weather.html");
		//initWidget(cont);
	}
	
	

	@Override
	public HasSelectHandlers getSearchButton() {
		return searchButton;
	}

	@Override
	public HasValue<String> getSearchField() {
		return searchField;
	}

	@Override
	public void setPresenter(IMenuPresenter presenter) {		
		this.presenter = presenter;
	}

	@Override
	public HasSelectHandlers getMainButton() {
		return mainButton;
	}

	@Override
	public HasSelectHandlers getCatalogButton() {
		return catalogButton;
	}

	@Override
	public HasSelectHandlers getAddressButton() {
		return addressButton;
	}

	@UiHandler("searchButton")
	public void onSearch(SelectEvent event) {
		if (searchField.getValue() != null && searchField.getValue().length() > 0) {
//			Info.display("Search", "Search '" + searchField.getValue() + "' started ()...");
			//placeController.goTo(newPlace);
			History.newItem(CatalogPlace.VIEW_HISTORY_TOKEN + ":" + searchField.getValue());
		}
	}
	
	@UiHandler("mainButton")
	public void onMain(SelectEvent event) {
		History.newItem(MainPlace.VIEW_HISTORY_TOKEN + ":");
	}
	
	@UiHandler("catalogButton")
	public void onCatalog(SelectEvent event) {
		History.newItem(CatalogPlace.VIEW_HISTORY_TOKEN + ":");
	}
	
	@UiHandler("addressButton")
	public void onAddress(SelectEvent event) {
		History.newItem(AddressPlace.VIEW_HISTORY_TOKEN + ":");
	}
	
	@Override
	public void setLoginData(String userName, String loginURL) {
//		if ( userName == null || userName.length() == 0  ) {
//			this.loginURL= loginURL;
//		} else {
//			loginButton.setText(userName);
//		}
		
	}

	@Override
	public TextButton getLoginButton() {
		return loginButton;
	}


	@Override
	public TextButton getLoadCatalogButton() {
		return loadCatalogButton;
	}



	@Override
	public void setUserName(String userName) {
		this.userName.setLabel(userName);
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			
			@Override
			public void execute() {
				cont.forceLayout();
			}
		});
	}



	@Override
	public Widget asWidget() {
		return cont;
	}
	
	
}
